package pers.hl.module_main.mine

import android.os.Bundle
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.lib_global.model.bean.Book
import pers.hl.lib_global.model.bean.BookTag
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.search.BookAdapter

class TagFragment : BaseBookListFragment() {

    protected lateinit var tag: BookTag

    companion object {

        const val KEY_TAG_DATA = "key_tag_data"

        fun newInstance(tag: BookTag): TagFragment {
            val fragment = TagFragment()
            val bundle = Bundle()
            bundle.putParcelable(KEY_TAG_DATA, tag)
            fragment.arguments = bundle
            fragment.tag = tag
            return fragment
        }
    }

    override fun getTitle(): String {
        return tag.name.toString()
    }

    override fun beforeSetAdapter() {
    }

    override fun createAdapter(): BaseLibQuickAdapter<Book, BaseViewHolder> {
        return BookAdapter(mutableListOf())
    }

    override fun initData(savedInstanceState: Bundle?) {
        mViewModel.fetchBookByTag(tag.id).observe(this, Observer {
            it.apply {
                mAdapter.setList(it)
            } ?: mAdapter.setEmptyView()
        })
    }

    override fun killSelf() {
        fragmentManager?.popBackStackImmediate()
    }

    override fun onBackPressed() {
        killSelf()
    }
}
